import {firstUpperCase} from '@/utils/index'
const getBtn = (
  txt,
  funType,
  row,
  h,
  context,
  isDisabled = false,
  type = 'primary',
  parent,
  parentIndex
) => {
  return (
    <el-link
      class='mr10'
      underline={false}
      type={`${type}`}
      disabled={isDisabled}
      onClick={() => {
        if (funType === 'backApproval') console.log(firstUpperCase(funType), row)
        return context[`on${firstUpperCase(funType)}`](row, parent, parentIndex)
      }
      }
    >
      {txt}
    </el-link>
  )
}

export const columns = ctx => {
  const checkStatus = ctx.searchForm.checkStatus
  const isDraft = checkStatus !== '3' && checkStatus !== '0'

  return [
    {
      prop: isDraft ? 'code' : 'productCode',
      label: '产品编码'
    },
    {
      prop: 'businessTypeName',
      label: '业务类型',
      hide: isDraft
    },
    {
      prop: 'name',
      label: '产品名称'
    },
    {
      prop: 'typeName',
      label: '产品类型',
      hide: isDraft
    },
    {
      prop: 'salesTypeName',
      label: '销售单类型',
      hide: isDraft,
      toolTip: true
    },
    {
      prop: 'apartmentName',
      label: '可售部门',
      toolTip: true,
      hide: isDraft,
      width: 300
    },
    {
      label: '理论成本',
      hide: isDraft,
      render: (h, row) => {
        if (Number(+row.theoryCostPrice).toFixed(2) === '0.00') {
          return <span>-</span>
        } else {
          return <span>{row.type === '3' ? `${row.theoryCostPrice} %` : `￥ ${row.theoryCostPrice}`}</span>
        }
      }
    },
    {
      label: '实际成本',
      hide: isDraft,
      render: (h, row) => {
        if (Number(+row.practiceCostPrice).toFixed(2) === '0.00') {
          return <span>-</span>
        } else {
          return <span>{row.type === '3' ? `${row.practiceCostPrice} %` : `￥ ${row.practiceCostPrice}`}</span>
        }
      }
    },
    {
      label: '产品面值',
      hide: isDraft,
      render: (h, row) => {
        if (Number(+row.parValue).toFixed(2) === '0.00') {
          return <span>-</span>
        } else {
          return <span>{`￥ ${row.parValue}`}</span>
        }
      }
    },
    {
      prop: 'exchangeRangeName',
      label: '兑换范围',
      hide: isDraft
    },
    {
      prop: 'operator',
      label: '操作人',
      hide: !isDraft
    },
    {
      prop: 'updateTime',
      label: '修改时间',
      hide: !isDraft
    },
    {
      label: '操作',
      fixed: 'right',
      width: 300,
      render: (h, row, index) => {
        let isDisabled = +row.isSale === 1

        let btns = []
        let tjsp = getBtn('提交审批', 'submit', row, h, ctx)
        let back = getBtn('撤回审批', 'back', row, h, ctx)
        let copy = getBtn('复制', 'copy', row, h, ctx)
        let edit = getBtn('编辑', 'edit', row, h, ctx)
        let detail = getBtn('详情', 'detail', row, h, ctx)
        let set = getBtn('设为可售', 'set', row, h, ctx, isDisabled)
        let del = getBtn('删除', 'del', row, h, ctx)
        // 3:已通过，2：被驳回，1：审批中，4：已撤销，0：未提交
        if (checkStatus === '3') {
          btns = [copy, detail, set]
        } else if (['2', '4', '0'].includes(checkStatus)) {
          btns = [copy, edit, tjsp, detail, del]
        } else if (checkStatus === '1') {
          btns = [copy, back, detail]
        }
        return btns
      }
    }
  ]
}

// newProduct 校验规则
export const rules = ctx => {
  const ruleList = Object.keys(ctx.form)
  let rulesObj = {}
  ruleList.forEach(item => {
    rulesObj[item] = {
      required: true,
      message: '*为必填项，不能为空',
      trigger: 'change'
    }
  })
  return rulesObj
}

// 产品配置的兑换范围
export const PRODUCT_EXCHANGE = [
  {
    name: '线上',
    value: '1'
  },
  {
    name: '线下',
    value: '2'
  },
  {
    name: '心选膨胀',
    value: '3'
  }
]
export const BUSINESS_TOTAL_TYPE = [
  {name: '卡业务类型', value: 'card'},
  {name: '券业务类型', value: 'coupon'}
]
